<template>
  <div style="width: 100%; height: 100%">
    <!-- 查询框 -->
    <div>
      <el-tag >工作类别</el-tag>
      <el-input v-model="keyword" style="width: 300px; margin-left: 10px; margin-right: 10px" placeholder="请输入关键词"></el-input>
      <el-button type="primary" @click="fetchAccounts">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>

    <!-- 操作框 -->
    <div style="margin: 10px 0">
<!--      <el-button type="primary" plain @click="showAddDialog">新增</el-button>-->
      <el-button type="danger" plain @click="delBatch">批量删除</el-button>
    </div>

    <!-- 账号列表 -->
    <!-- 表格 -->
    <div style="margin: 10px 0">
      <el-table :data="jobData" stripe :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666' }"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="job_id" label="序号" width="70" align="center"></el-table-column>
        <el-table-column prop="job_title" label="职位名称"></el-table-column>
        <el-table-column prop="job_description" label="职位描述"></el-table-column>
        <el-table-column prop="requirements" label="职位要求"></el-table-column>
        <el-table-column prop="location" label="工作地点"></el-table-column>
        <el-table-column prop="salary" label="薪资"></el-table-column>
        <el-table-column prop="company" label="公司介绍">
          <template slot-scope="scope">
          <el-popover
              placement="right"

              trigger="click">
            <el-table :data="companies">
              <el-table-column width="100" property="company_name" label="公司名称"></el-table-column>
              <el-table-column width="100" property="industry" label="行业"></el-table-column>
              <el-table-column width="100" property="website" label="网址"></el-table-column>
              <el-table-column width="100" property="description" label="描述"></el-table-column>
              <el-table-column width="100" property="contact_person" label="联系人"></el-table-column>
              <el-table-column width="100" property="contact_email" label="电子邮箱"></el-table-column>
              <el-table-column width="100" property="contact_phone" label="联系电话"></el-table-column>
              <el-table-column width="150" property="address" label="地址"></el-table-column>
            </el-table>
            <el-button slot="reference" @click="selectCompany(scope.row.company_id)">查看公司介绍</el-button>
          </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="handleDelete(scope.row.job_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>


    <!-- 新增/编辑账号模态框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%" >
      <el-form :model="editItem" label-width="100px" :rules="rule">

        <el-form-item label="工作名称">
          <el-input type="job_title"  v-model="editItem.job_title"></el-input>
        </el-form-item>
        <el-form-item label="工作描述">
          <el-input type="job_description" v-model="editItem.job_description"></el-input>
        </el-form-item>
        <el-form-item label="工作要求">
          <el-input type="requirements" v-model="editItem.requirements"></el-input>
        </el-form-item>
        <el-form-item label="工作地点">
          <el-input type="location" v-model="editItem.location"></el-input>
        </el-form-item>
        <el-form-item label="工作薪资">
          <el-input type="salary" v-model="editItem.salary"></el-input>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSaveEdit">{{ isEditing ? '保存' : '新增' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      student:'',
      searchAccount: '',
      jobData: [], // 信息列表
      companies:[],//企业信息列表
      currentPage: 1, // 当前页数
      pageSize: 5, // 每页显示的条数
      totalItems: 0, // 总条数
      keyword: '', // 查询关键词
      dialogVisible: false, // 账号信息模态框可见性
      editItem: { // 编辑/新增的数据
        job_id:'',
        company_id: '',
        job_title: '',
        job_description: '',
        requirements: '',
        location: '',
        salary: '',
      },
      isEditing: false, // 是否为新增模式
      rule:{
        email:[
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      }
    };
  },
  computed: {
    dialogTitle() {
      return this.isEditing ? '编辑信息' : '新增信息'; //false为新增，true为编辑
    }
  },
  mounted() {
    this.fetchAccounts();
  },
  methods: {
    // 获取账号信息
    fetchAccounts() {

      let url = 'http://localhost:8080/JobManger/selectAll';
      let params = {
        page: this.currentPage,
        size: this.pageSize
      };


      if (this.keyword) {
        // 如果输入了关键词，则根据选择的查询条件发送相应的请求
        // 构建带查询参数的URL

        axios.get('http://localhost:8080/JobManger/selectByTitle', {
          params: {
            job_title: this.keyword
          }
        })
            .then(response => {
              this.jobData = response.data.data;
            })
            .catch(error => {
              console.error('Error fetching accounts:', error);
            });

      }else {
        axios.get(url,{ params } )
            .then(response => {
              const data = response.data.data;
              this.jobData = data;
              this.totalItems = data.length;

            })
            .catch(error => {
              console.error('Error fetching accounts:', error);
            });
      }



    },

    selectCompany(company_id){
      axios.get('http://localhost:8080/JobManger/selectByCompany_id', {
        params: {
          company_id: company_id
        }
      })
          .then(response => {
            this.companies = response.data.data;
          })
          .catch(error => {
            console.error('Error fetching accounts:', error);
          });
    },
    reset() {//重置
      this.keyword = '';
      this.currentPage = 1;
      this.pageSize = 5;
      this.fetchAccounts();
    },

    handleSelectionChange(val) {//选择的行
      this.selectedRows = val.map(row => row.user_id);
    },

    closeDialog() {
      this.dialogVisible = false;
      this.isEditing = false;
    },
    handleAdd() {
      // 实现新增功能的逻辑
      axios.post("http://localhost:8080/User/add", this.editItem)

          .then(response => {
            console.log(this.editItem)
            // 处理新增成功逻辑
            this.$message.success("新增成功");
            this.dialogVisible = false;
            this.fetchAccounts(); // 刷新数据
          })
          .catch(error => {
            // 处理新增失败逻辑
            console.error('Error adding new item:', error);
            this.$message.error("新增失败");
          });
    },
    handleEdit(row) {
      // 将选中行的数据赋值给编辑的数据项
      this.editItem = Object.assign({}, row);
      // 设置编辑状态为true
      this.isEditing = true; // 设置编辑状态为true，表示编辑
      // 打开新增/编辑弹窗
      this.dialogVisible = true;
    },
    handleSaveEdit() {
      // 发送编辑/新增请求到后端
      if (this.isEditing) {
        // 编辑状态下发送编辑请求
        axios.put("http://localhost:8080/JobManger/edit", this.editItem)
            .then(response => {
              // 处理编辑成功逻辑
              this.$message.success("编辑成功");
              this.closeDialog();
              this.fetchAccounts(); // 刷新数据
            })
            .catch(error => {
              // 处理编辑失败逻辑
              console.error('Error editing item:', error);
              this.$message.error("编辑失败");
            });
      } else {
        // 新增状态下发送新增请求
        this.handleAdd();
      }
    },
    handleDelete(user_id) {
      this.$confirm('此操作将永久删除该条目，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.delete(`http://localhost:8080/JobManger/delete/${user_id}`)
            .then(response => {
              this.$message.success('删除成功');
              this.fetchAccounts(); // 刷新数据
            })
            .catch(error => {
              console.error('Error deleting item:', error);
              this.$message.error('删除失败');
            });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    delBatch() {
      // 确认是否删除选中的行
      this.$confirm('此操作将永久删除所选条目，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 批量删除操作
        axios.delete(`http://localhost:8080/JobManger/deleteJobBatch`, {  data: this.selectedRows })
            .then(response => {
              this.$message.success('批量删除成功');
              this.fetchAccounts(); // 刷新数据
              this.selectedRows = []; // 清空选中的行
            })
            .catch(error => {
              console.error('Error deleting batch:', error);
              this.$message.error('批量删除失败');
            });
      }).catch(() => {
        this.$message.info('已取消批量删除');
      });
    }
  }
};
</script>

<style scoped>
.table-container {
  margin-top: 20px;
}
</style>

